import 'package:flutter_shop/cons/global_cons.dart';
import 'package:flutter_shop/eventbus/event_bus.dart';
import 'package:flutter_shop/pages/base/base_presenter.dart';
import 'package:flutter_shop/pages/cart/cart_router.dart';
import 'package:flutter_shop/pages/cart/cart_tab_page/entity/cart_goods_data.dart';
import 'package:flutter_shop/pages/cart/cart_tab_page/provider/cart_provider.dart';
import 'package:flutter_shop/pages/common/index_page/index_page.dart';
import 'package:flutter_shop/pages/home/goods_detail/entity/goods_detail_data.dart';
import 'package:flutter_shop/pages/home/goods_detail/goods_detail_model.dart';
import 'package:flutter_shop/pages/home/goods_detail/goods_detail_contract.dart';
import 'package:flutter_shop/routers/navigator_utils.dart';
import 'package:flutter_shop/utils/utils.dart';
import 'package:provider/provider.dart';

class GoodsDetailPresenter extends BasePresenter<GoodsDetailView> {
  GoodsDetailModel mModel = GoodsDetailModel();

  ///请求获取商品详情
  void getGoodsDetail(String id) {
    mModel.getGoodsDetail(id).then((val) {
      mView.showPageData(val);
    }).catchError((error) {
      mView.showError(error.message);
    });
  }

  ///去购物车
  void goCartTab() {
    EventBus().post(EventMessage(IndexPage.CHANGE_INDEX, int1: 2));
    NavigatorUtils.goBack(mContext);
  }

  ///添加商品到购物车
  void addGoodsToCart(GoodsDetailDataGoodInfo goodInfo) {
    Provider.of<CartProvider>(mContext, listen: false).add(goodInfo);
  }

  ///立即购买
  void nowBuy(GoodsDetailDataGoodInfo goodInfo) {
    List<CartGoodsData> list = [];
    CartGoodsData cartGoodsEntity = new CartGoodsData();
    cartGoodsEntity.goodsId = goodInfo.goodsId;
    cartGoodsEntity.goodsName = goodInfo.goodsName;
    cartGoodsEntity.count = 1;
    cartGoodsEntity.price = goodInfo.presentPrice;
    cartGoodsEntity.oriPrice = goodInfo.oriPrice;
    cartGoodsEntity.image = goodInfo.image1;
    cartGoodsEntity.checked = true;
    list.add(cartGoodsEntity);

    NavigatorUtils.pushCheckLogin(mContext, CartRouter.orderConfirm, params: {GlobalCons.KEY_DATA: Utils.parseObj2Json(list)});
  }
}
